// var note=	'<div class="addnote">'
// 		+		'<div class="note-lead">'
// 		+			'<span id="note-word">笔记</span>'
// 		+			'<span class="close"><i class="fa fa-times"></i></span>'
// 		+		'</div>'
// 		+		'<div class="note-text">'
// 		+			'<textarea></textarea>'
// 		+		'</div>'
// 		+		'<form action="#" id="note-form">'
// 		+			'<button type="button" name="note-save" id="note-submit">Submit</button>'
// 		+			'<input type="hidden" name="saved-note" value=""></input>'
// 		+		'</form>'
// 		+	'</div>';

$(function(){
	var $addnote=$("[class=addnote]");
	var note_width=$addnote.width();
	var note_height=$addnote.height();
	var all_width=$("[class=top-wrap]").width();
	var all_height=650;
	var $save=$("#note-submit");
	var height=((all_height-note_height)/2)>0?(all_height-note_height)/2:0;			//定位的高度
	var left=(all_width-note_width)/2;					//定位的宽度
	var isclose=true;
	var curHeight=$addnote.offset().top;
	var $notebutton=$(".notebook");
	$notebutton.bind("click",function(){
		if (isclose) {
			$addnote.show();
			$addnote.css("left",left+'px');
			$addnote.css("top",-note_height+'px');
			down($addnote,curHeight,height);
			isclose=false;
		}
	});
	var $close=$(".close");
	$close.click(function(){
		$addnote.hide(500);
		isclose=true;
	});
	$save.click(function(){
		var saved=$("#note-self").html();
		$("[name=saved-note]").val(saved);
		var url=$("#nextque").attr("action");
		$.ajax({
			dataType:'json',
			url:url+'',
			data:$("#note-form").serialize(),
			method:'post',
			complete:function(data){
				$addnote.hide(500);
				isclose=true;
			}
		})
	});
	var $add=$(".add-note-text")||null;
	$add.click(function(){
		var text=$(".text-note").text();
		text='<br>'+text;
		$("#note-self").html($("#note-self").html()+text);
		if (!$addnote.is(':visible')) {
			$("#note-submit").click();
		}
	})
});



function speed(a,time){
	return a*time;
}


function down($demo,curHeight,height){
	if (curHeight<=height) {
		$demo.css("top",curHeight+'px');
		var t=0;
		var a=.1;
		var v=0;
		var times=setInterval(
			function(){
				if (parseInt($demo.css("top"))<=height) {
					t+=1;
					$demo.stop(true).css({top:0.5*a*t*t+curHeight+'px'},5);
				}else{
					v0=0.5*speed(a,t);
					v=v0;
					t=0;
					clearInterval(times);
					curHeight=$demo.offset().top;
					var up=setInterval(
						function(){
							if (v>0) {
								t+=1;
								v=v0-speed(a,t);
								$demo.stop(true).css({top:curHeight-0.5*a*t*t+'px'},5)
							}else{
								curHeight=$demo.offset().top;
								clearInterval(up);
								down($demo,curHeight, height);
							}
						},5)
				}
			},5)
	}
}


// 将文字转化为段落并删除前后空格
function changeType(val){
	var lines=[];
	var returnType='';
	lines=val.split(/\n\s*/);
	for (var i = 0; i < lines.length; i++) {
		lines[i]=lines[i].trim();
		returnType+='<p>'+lines[i]+'</p>';
	}
	return returnType;
}